﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace DARE
{
    public class CLineText3D: CSprite3D
    {
        #region fields

        private string m_text;
        private SpriteFont m_spriteFont = null;
        private Texture2D m_textureTmp;
        private Color m_bgColor = new Color(0, 0, 0, 0);

        #endregion

        #region properties

        public SpriteFont SpriteFont { get { return m_spriteFont; } set { m_spriteFont = value; Text = m_text; Actualize(); } }

        public Color BackgroundColor { get { return m_bgColor; } set { m_bgColor = value; Actualize(); } }

        public override Color Color
        {
            get
            {
                return base.Color;
            }
            set
            {
                base.Color = value;
                Actualize();
            }
        }

        public override Texture2D Texture
        {
            get { return m_textureTmp; }
            set { m_textureTmp = value; Actualize(); }
        }

        public string Text
        {
            get { return m_text; }
            set
            {
                if (m_spriteFont == null)
                    throw new Exception("A spritefont must be set to draw text!!!");
                m_text = value;
                Actualize();
            }
        }

        #endregion

        #region ctor

        public CLineText3D()
            :base()
        {
            m_spriteFont = CDare.Instance.DebugMgr.DebugConsole.SpriteFont;
        }
        public CLineText3D(string name)
            : this(name, null)
        {

        }
        public CLineText3D(string name, CRender3D render3D)
//            : base(name, render3D)
        {
            m_name = name;
            m_spriteFont = CDare.Instance.DebugMgr.DebugConsole.SpriteFont;
        }

        public override CSprite3D Clone()
        {
            CLineText3D lineText3d = new CLineText3D(m_name);

            lineText3d.Offset = m_offset;
            lineText3d.Text = m_text;
            return lineText3d;
        }

        #endregion

        #region methods

        protected override void Actualize()
        {
            RenderTarget2D rt;
            if (m_text != null && m_text != "")
            {
                Vector2 v = m_spriteFont.MeasureString(m_text);
                if ((int)v.X > 4096 || (int)v.Y > 4096)
                {
                    Console.WriteLine("WARNING!!: CLineText3D: texture size of more than 4096 !!!");
                    return;
                }
                rt = new RenderTarget2D(CDare.Instance.GraphicsDevice, (int)v.X, (int)v.Y);
                if (v.X != 0 && v.Y != 0)
                {
                    m_size.X = v.X;
                    m_size.Y = v.Y;
                }
            }
            else if (m_textureTmp != null)
            {
                rt = new RenderTarget2D(CDare.Instance.GraphicsDevice, m_textureTmp.Width, m_textureTmp.Height);
                if (m_textureTmp.Width != 0 && m_textureTmp.Height != 0)
                {
                    m_size.X = m_textureTmp.Width;
                    m_size.Y = m_textureTmp.Height;
                }
            }
            else
            {
                if (m_size.X == 0 || m_size.Y == 0)
                {
                    m_size.X = 1;
                    m_size.Y = 1;
                }
                rt = new RenderTarget2D(CDare.Instance.GraphicsDevice, (int)m_size.X, (int)m_size.Y);
            }
            m_rect.Width = (int)m_size.X;
            m_rect.Height = (int)m_size.Y;
            lock (CDare.Instance.GraphicsDevice)
            {
                CDare.Instance.GraphicsDevice.SetRenderTarget(rt);

                CDare.Instance.GraphicsDevice.Clear(new Color(0, 0, 0, 0));
                CDare.Instance.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.AnisotropicClamp, DepthStencilState.Default, RasterizerState.CullNone);
                if (m_textureTmp != null)
                    CDare.Instance.SpriteBatch.Draw(m_textureTmp, m_rect, m_bgColor);
                if (m_text != null)
                    CDare.Instance.SpriteBatch.DrawString(m_spriteFont, m_text, Vector2.Zero, m_color);
                CDare.Instance.SpriteBatch.End();
                CDare.Instance.GraphicsDevice.SetRenderTarget(null);
            }
            m_texture = rt;
        }

        #endregion

    }
}
